
package com.ruoyi.controller;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.pojo.DriverEntryApproval;
import com.ruoyi.pojo.DriverUser;
import com.ruoyi.service.DriverEntryApprovalService;
import com.ruoyi.service.DriverUserService;
import com.ruoyi.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/driver/approve")
public class DriverUserController {
    @Autowired
    private DriverUserService driverUserService;

    @Autowired
    private DriverEntryApprovalService driverEntryApprovalService;

    //查询所有没有审核的车主信息
    @GetMapping("/selectByStatus")
    public List<DriverUser> updateByCar() {
        List<DriverUser> list = driverUserService.selectByStates();
        return list;
    }

    /**
     * 根据id查询想要查看的司机的该条数据
     *
     * @param id
     * @return
     */
    @GetMapping("/updateById")
    public DriverUser updateCarById(@RequestParam("id") Long id) {
        DriverUser driverUser = driverUserService.selectById(id);
        return driverUser;
    }


    /**
     * 审核通过
     *
     * @param id
     * @return
     */
    @PostMapping("/pass")
    public String PassTheExamination(@RequestParam("id") Long id) {
        DriverUser driverUser = driverUserService.selectById(id);
        driverUser.setState(Utils.yes);
        driverUserService.updateById(driverUser);
        DriverEntryApproval driverentryapproval = BeanUtil.toBean(driverUser, DriverEntryApproval.class);
        boolean result = driverEntryApprovalService.installLogs(driverentryapproval);
        if (result) {
            return "审核成功";
        }
        return "审核失败";
    }

    /**
     * 审核驳回
     *
     * @param driverUsers
     * @return
     */
    @PostMapping("/rejected")
    public boolean reviewAndReject(@RequestBody DriverUser driverUsers) {
        DriverUser driverUser = driverUserService.selectById(driverUsers.getId());
        driverUser.setState(Utils.no);
        driverUserService.updateById(driverUser);
        DriverEntryApproval driverEntryApproval = BeanUtil.toBean(driverUser, DriverEntryApproval.class);
        driverEntryApproval.setCauseOfRejection(driverUsers.getCauseOfRejection());
        driverEntryApprovalService.installLogs(driverEntryApproval);
        return true;
    }
}

